package com.woniuxy.creditsystem.financialmag.service.impl;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.woniuxy.creditsystem.common.bean.MoneySetInfo;
import com.woniuxy.creditsystem.common.bean.PageRequest;
import com.woniuxy.creditsystem.common.bean.ResultMsg;
import com.woniuxy.creditsystem.common.bean.UserInfo;
import com.woniuxy.creditsystem.common.mapper.MoneySetInfoMapper;
import com.woniuxy.creditsystem.financialmag.service.IMoneySetInfo;
import org.apache.ibatis.annotations.Param;
import org.apache.poi.hssf.usermodel.HSSFCell;
import org.apache.poi.hssf.usermodel.HSSFRow;
import org.apache.poi.hssf.usermodel.HSSFSheet;
import org.apache.poi.hssf.usermodel.HSSFWorkbook;
import org.apache.poi.ss.usermodel.CellStyle;
import org.apache.poi.ss.usermodel.CreationHelper;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Isolation;
import org.springframework.transaction.annotation.Transactional;

import javax.annotation.Resource;
import java.io.File;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import java.util.List;

/**
 * @author ：pengchufeng
 * @date ：Created in 2020/12/14
 * @description：资金方结算信息service接口实现
 */
@Transactional(readOnly = true)
@Service
public class MoneySetInfoImpl implements IMoneySetInfo {

    @Resource
    private MoneySetInfoMapper moneySetInfoMapper;

    /**
     * 条件查询所有资金方结算信息
     * @Param returnMoneyInfo 条件
     * @return
     */
    @Transactional(isolation = Isolation.REPEATABLE_READ,rollbackFor = Exception.class)
    @Override
    public PageInfo<MoneySetInfo> findAll(@Param("p")PageRequest request, @Param("moneySetInfo") MoneySetInfo moneySetInfo) {
        return PageHelper.startPage(request.getPageNum(),request.getLimit()).
                doSelectPageInfo(()->moneySetInfoMapper.findAll(request,moneySetInfo));
    }




    /**
     * 把页面传回来的table数据，写入到excel文件中
     * @param tableDataJson
     * @return
     */
    public ResultMsg write2excel(List<MoneySetInfo> tableDataJson , String path) throws IOException {
        if (tableDataJson==null){
            return new ResultMsg(201,"页面表格内容为空，没有数据可以导出！",null);
        }

        //创建表对象，即工作簿对象
        HSSFWorkbook workbook = new HSSFWorkbook();
        //第一个sheet对象demo1
        HSSFSheet sheet = workbook.createSheet("demo1");
        //行对象
        HSSFRow row = sheet.createRow(0);
        //单元格对象
        HSSFCell cell0 = row.createCell(0);
        cell0.setCellValue("订单号");
        HSSFCell cell1 = row.createCell(1);
        cell1.setCellValue("贷款申请日期");
        HSSFCell cell2 = row.createCell(2);
        cell2.setCellValue("借款金额");
        HSSFCell cell3 = row.createCell(3);
        cell3.setCellValue("期数");
        HSSFCell cell4 = row.createCell(4);
        cell4.setCellValue("代收渠道服务费率");
        HSSFCell cell5 = row.createCell(5);
        cell5.setCellValue("代收渠道服务费");
        HSSFCell cell6 = row.createCell(6);
        cell6.setCellValue("资金方");
        HSSFCell cell7 = row.createCell(7);
        cell7.setCellValue("计账日期（还款日）");
        HSSFCell cell8 = row.createCell(8);
        cell8.setCellValue("结算状态");
        HSSFCell cell9 = row.createCell(9);
        cell9.setCellValue("结算方式");

//        //解决日期显示不正常的问题：给第1行、第2列单元格写入日期
//        //用Excel表对象获取CreationHelper对象
//        CreationHelper createHelper = workbook.getCreationHelper();
//        //用Excel表对象创建CellStyle对象
//        CellStyle cellStyle = workbook.createCellStyle();
//        //给cellStyle对象设置Date格式
//        cellStyle.setDataFormat(createHelper.createDataFormat().getFormat("yyyy-MM-dd HH:mm:ss"));

        for (int i=0; i<tableDataJson.size(); i++){
            //行对象
            HSSFRow r = sheet.createRow(1+i);
            for (int j=0; j<10; j++){
                //单元格对象
                HSSFCell cell001 = r.createCell(0);
                cell001.setCellValue(tableDataJson.get(i).getNum());
                HSSFCell cell002 = r.createCell(1);
//                cell002.setCellStyle(cellStyle);
                cell002.setCellValue(tableDataJson.get(i).getStrAtime());
                HSSFCell cell003 = r.createCell(2);
                cell003.setCellValue(tableDataJson.get(i).getNewSumMoney());
                HSSFCell cell004 = r.createCell(3);
                cell004.setCellValue(tableDataJson.get(i).getStrNum());
                HSSFCell cell005 = r.createCell(4);
                cell005.setCellValue(tableDataJson.get(i).getNewHandleCost());
                HSSFCell cell006 = r.createCell(5);
                cell006.setCellValue(tableDataJson.get(i).getPlatCost());
                HSSFCell cell007 = r.createCell(6);
                cell007.setCellValue(tableDataJson.get(i).getDitchAllname());
                HSSFCell cell008 = r.createCell(7);
//                cell008.setCellStyle(cellStyle);
                cell008.setCellValue(tableDataJson.get(i).getStrDate());
                HSSFCell cell009 = r.createCell(8);
                cell009.setCellValue(tableDataJson.get(i).getAlsoStatus());
                HSSFCell cell010 = r.createCell(9);
                cell010.setCellValue(tableDataJson.get(i).getPayType());
            }
        }

        FileOutputStream os = null;
        String filePathName = path + File.separator + "资金方结算" + System.currentTimeMillis() + ".xls";
        File file=new File(path);
        if (!file.exists()){
            file.mkdirs();
        }
        try {
            os = new FileOutputStream(filePathName);
        } catch (FileNotFoundException e) {
            e.printStackTrace();
        }
        //		workbook1.write(os);
        try {
            workbook.write(os);
        } catch (IOException e) {
            e.printStackTrace();
        }
        os.flush();
        os.close();

        ResultMsg resultMsg=  new ResultMsg(200,"页面表格数据已经写入到"+filePathName,null);
        return resultMsg;

    }






}
